package com.springboot.cloud.demos.producer.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.springboot.cloud.demos.producer.dao.MenuMapper;
import com.springboot.cloud.demos.entity.po.Menu;
import com.springboot.cloud.demos.producer.service.IMenuService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
@RefreshScope
public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu> implements IMenuService {

    @Value("${producer.menu:123}")
    private String value;

    @Override
    @CacheEvict(value = "menu",allEntries=true)
    public boolean add(Menu menu) {
        return this.save(menu);
    }

    @Override
    @CacheEvict(value = "menu", key = "#root.targetClass+'-'+#id")
    public boolean delete(String id) {
        return this.removeById(id);
    }

    @Override
    @CacheEvict(value = "menu", key = "#root.targetClass+'-'+#menu.id")
    public boolean update(Menu menu) {
        return this.updateById(menu);
    }

    @Override
    @Cacheable(value = "menu", key = "#root.targetClass+'-'+#id")
    public Menu get(String id) {
        log.info("value:{}", value);
        return this.getById(id);
    }

    @Override
    @Cacheable(value = "menu", key = "#root.targetClass+'list'")
    public List<Menu> queryAll() {
        QueryWrapper<Menu> queryWrapper = new QueryWrapper();
        queryWrapper.orderByAsc("sort");
        return this.list(queryWrapper);
    }
}
